package com.vip.oauth2resource.util;

import org.springframework.security.core.context.SecurityContextHolder;

/**
 * Oauth2资源工具类
 *
 * @author LEON
 */
public class UserHelper {

    /**
     * 获取上下文中当前用户信息
     *
     * @return
     */
    public static CustomUserDetail getUser() {
        Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        /**
         * 如果是匿名用户直接返回
         */
        if ("anonymousUser".equals(obj.toString())) {
            return new CustomUserDetail().setUsername("anonymousUser").setName("匿名用户");
        }
        return (CustomUserDetail) obj;
    }
}
